﻿var CurrentFlowName = {};
var CurrentAd = {};

Ext.onReady(function () {
    PageSafe();

    //流程列表
    var flownamestore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Flow.FlowView', 'GetBaseFlow'),
        root: 'ResponseObject',
        fields: ['PID', 'FlowN', 'Creater', 'LastUpdate', 'NTime', 'CreateName.UserName'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var flownamesm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = flownamegrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentFlowName = k[0].json;
                }
            },
            rowdeselect: function () {
                CurrentFlowName = {};
            }
        }
    });
    var flownamegrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        autoHeight: true,
        sm: flownamesm,
        loadMask: true, //加载
        columns: [
       flownamesm,
       { id: 'FlowN', header: '流程名称', width: 100, dataIndex: 'FlowN', sortable: true }
   ],
        store: flownamestore
    });

    //未审核广告
    var docstore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('Flow.FlowView', 'GetDocInfo_All'),
        root: 'ResponseObject',
        fields: ['PID', 'DocName', 'DocPrice', 'DocCustom', 'IsGoods', 'GoodsCount', 'GoodsName', 'Remark', 'MoreInfo', 'UserID', 'DocState', 'DState', 'LastUpdate', 'NTime'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var docsm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = docgrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentAd = k[0].json;
                }
            },
            rowdeselect: function () {
                CurrentAd = {};
            }
        }
    });
    function NumFormat(s, m) {
        m.css = 'RedNum';
        return s;
    }
    var docgrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 500,
        sm: docsm,
        loadMask: true, //加载
        columns: [
       docsm,
       { id: 'DocName', header: '申请单名称', width: 100, dataIndex: 'DocName', sortable: true },
       { id: 'DocPrice', header: '价格', width: 60, dataIndex: 'DocPrice', sortable: true },
       { id: 'DocCustom', header: '客户名称', width: 100, dataIndex: 'DocCustom', sortable: true },
       { id: 'IsGoods', header: '是否物品', width: 80, dataIndex: 'IsGoods', sortable: true },
       { id: 'GoodsName', header: '物品名称', width: 80, dataIndex: 'GoodsName', sortable: true },
       { id: 'GoodsCount', header: '数量', width: 60, dataIndex: 'GoodsCount', sortable: true },
        { id: 'DState', header: '状态', width: 60, dataIndex: 'DState', sortable: true },
       { id: 'NTime', header: '日期', width: 100, dataIndex: 'NTime', sortable: true },
       { id: 'NTime', header: '查看', width: 100, dataIndex: '', sortable: true, renderer: function () {
           return '<a href="#" onclick="DownDoc();">下载查看</a>';
       } 
       }
   ],
        store: docstore
    });


    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '绑定流程',
            iconCls: 'flowbind-icon',
            tbar: ['->', { text: '应用流程', iconCls: 'yy-icon', handler: function () {
                if (CurrentAd.PID == null) { Ext.Msg.alert("提示信息", "请选择需要绑定流程的申请单"); return; }
                if (CurrentFlowName.PID == null) { Ext.Msg.alert("提示信息", "请选择需要绑定的流程"); return; }
                var re = new JsonRequest();

                re.params.flowinfo = CurrentFlowName;
                re.params.info = CurrentAd;
                Ext.Ajax.request({
                    url: Ext.getClassUrl('Advertisement.AdvertisementView', 'BindFlowDoc'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (!obj.HasError) {
                            Ext.Msg.alert("提示信息", "流程绑定成功");
                            docstore.load();
                        }
                        else {
                            Ext.Msg.alert("提示信息", "流程绑定失败，请联系管理员");
                        }
                    }
                });
            }
            }],
            layout: 'border',
            items: [{
                region: 'west',
                width: 200,
                title: '流程列表',
                iconCls: 'flow-icon',
                style: 'padding:2px',
                items: flownamegrid
            }, {
                region: 'center',
                style: 'padding:2px',
                title: '未审核申请单列表',
                iconCls: 'Advertisement-icon',
                items: docgrid
            }]
        }],
        listeners: {
            render: function () {
                flownamestore.load();
                docstore.load();
            }
        }
    });
});

var DownDoc = function () {
    window.open("../" + CurrentData.MoreInfo);
}